﻿/* 事件集合 */

/* 初始化加载 */
$(function () {
    $(".left_header .left_header_title .titlename").html(ISS_MAP_CENTER_NAME);
});

//返回按钮
$("#returnGotobox").click(function () {
    //    window.location.href = decodeURI(ISS.utils.getParameter("url"));
    //去触发IframeForGoto的返回按钮
    var returnBtn = window.parent.document.getElementById("realReturn");
    returnBtn.click();
});

//goto与search标签切换
$(".right_header .right-route-to").click(function () {
    $("#choice_goto").hide();
    $("#left-panel").show();
    //颜色
    $("#searchinput_end").css("color", "#000");
    //传值
    $("#searchinput_start").val('');
    $("#searchinput_end").val(ISS_MAP_CENTER_NAME);
    ISS_MAP_SEARCH_END.Lng = ISS_MAP_CENTER_LNG;
    ISS_MAP_SEARCH_END.Lat = ISS_MAP_CENTER_LAT;
    ISS_MAP_SEARCH_END.Name = ISS_MAP_CENTER_NAME;
    //调用百度定位-我的位置
    //    navigator.geolocation.getCurrentPosition(ISS_MAP.TranPoint); //浏览器定位 
    LocalGeocoder('to');
});
$(".right_header .right-route-from").click(function () {
    $("#choice_goto").hide();
    $("#left-panel").show();
    //颜色
    $("#searchinput_start").css("color", "#000");
    //传值
    $("#searchinput_start").val(ISS_MAP_CENTER_NAME);
    ISS_MAP_SEARCH_START.Lng = ISS_MAP_CENTER_LNG;
    ISS_MAP_SEARCH_START.Lat = ISS_MAP_CENTER_LAT;
    ISS_MAP_SEARCH_START.Name = ISS_MAP_CENTER_NAME;
    $("#searchinput_end").val();
    //定位-我的位置
    //    navigator.geolocation.getCurrentPosition(ISS_MAP.TranPoint); //浏览器定位 
    LocalGeocoder('from');
});
$(".route-header .searchbox-content-button.right-button.cancel-button.loading-button").click(function () {
    $("#left-panel").hide();
    $("#choice_goto").show();
    //传值
    $("#searchinput_start").val('');
    $("#searchinput_end").val('');
});

//tab切换
$(".route-tabs .tab-item").click(function () {
    var oldAttr = $("#route-searchbox-content").attr("class");
    if ($(this).attr("data-index") == "drive") {
        ISS_MAP_SEARCHTYPE = 1;
        oldAttr = oldAttr.replace("bus", "").replace("walk", "") + " drive";
    } else if ($(this).attr("data-index") == "walk") {
        ISS_MAP_SEARCHTYPE = 2;
        oldAttr = oldAttr.replace("bus", "").replace("drive", "") + " walk";
    } else if ($(this).attr("data-index") == "bus") {
        ISS_MAP_SEARCHTYPE = 3;
        oldAttr = oldAttr.replace("drive", "").replace("walk", "") + " bus";
    }
    $("#route-searchbox-content").attr("class", oldAttr);
});

//查询地点输入框事件
$("#searchinput_start,#searchinput_end").keydown(function () {
    $(this).css("color", "#000");
});
$("#searchinput_start,#searchinput_end").change(function () {
    if ($(this).attr("class").indexOf("route-start-input") >= 0) {
        ISS_MAP_SEARCH_START.Lng = "";
        ISS_MAP_SEARCH_START.Lat = "";
        ISS_MAP_SEARCH_START.Name = $("#searchinput_start").val();
    } else {
        ISS_MAP_SEARCH_END.Lng = "";
        ISS_MAP_SEARCH_END.Lat = "";
        ISS_MAP_SEARCH_END.Name = $("#searchinput_end").val();
    }
});

//查询地点点击事件
$("#searchinput_start,#searchinput_end").click(function () {
    if ($(this).attr("class").indexOf("route-start-input") >= 0
        && $(this).val() == "请输入起始点") {
        $("#searchinput_start").val('');
        $("#searchinput_start").css("color", "initial");
    } else if ($(this).val() == "请输入目的地") {
        $("#searchinput_end").val('');
        $("#searchinput_end").css("color", "initial");
    }
});

//切换起始点
$(".routebox .routebox-revert").click(function () {
    var tempPoint = {
        Lng: "",
        Lat: "",
        Name: ""
    };
    tempPoint.Lng = ISS_MAP_SEARCH_START.Lng;
    tempPoint.Lat = ISS_MAP_SEARCH_START.Lat;
    tempPoint.Name = ISS_MAP_SEARCH_START.Name;
    //--1
    ISS_MAP_SEARCH_START.Lng = ISS_MAP_SEARCH_END.Lng;
    ISS_MAP_SEARCH_START.Lat = ISS_MAP_SEARCH_END.Lat;
    ISS_MAP_SEARCH_START.Name = ISS_MAP_SEARCH_END.Name;
    //--2
    ISS_MAP_SEARCH_END.Lng = tempPoint.Lng;
    ISS_MAP_SEARCH_END.Lat = tempPoint.Lat;
    ISS_MAP_SEARCH_END.Name = tempPoint.Name;
    //颜色
    var tempcss = $("#searchinput_start").css("color");
    $("#searchinput_start").css("color", $("#searchinput_end").css("color"));
    $("#searchinput_end").css("color", tempcss);
    //--赋值文本框
    $("#searchinput_start").val(ISS_MAP_SEARCH_START.Name);
    $("#searchinput_end").val(ISS_MAP_SEARCH_END.Name);
});

//清空输入框
$(".route-start .input-clear").click(function () {
    $("#searchinput_start").val('');
    ISS_MAP_SEARCH_START.Lng = '';
    ISS_MAP_SEARCH_START.Lat = '';
    ISS_MAP_SEARCH_START.Name = '';
});
$(".route-end .input-clear").click(function () {
    $("#searchinput_end").val('');
    ISS_MAP_SEARCH_END.Lng = '';
    ISS_MAP_SEARCH_END.Lat = '';
    ISS_MAP_SEARCH_END.Name = '';
});

//搜索按钮
$("#search-button").click(function () {
    //    var str = "开始点:";
    //    str += "lng" + ISS_MAP_SEARCH_START.Lng + "," + ISS_MAP_SEARCH_START.Lat;
    //    str += "结束点:";
    //    str += "lng" + ISS_MAP_SEARCH_END.Lng + "," + ISS_MAP_SEARCH_END.Lat;
    //    alert(str);

    //判断输入是否完整
    if ((ISS_MAP_SEARCH_START.Lng == '' || ISS_MAP_SEARCH_START.Lat == '')
       && ISS_MAP_SEARCH_START.Name == '') {
        $("#searchinput_start").val("请输入起始点");
        $("#searchinput_start").css("color", "#ff7575");
        return;
    }
    if ((ISS_MAP_SEARCH_END.Lng == '' || ISS_MAP_SEARCH_END.Lat == '')
       && ISS_MAP_SEARCH_END.Name == '') {
        $("#searchinput_end").val("请输入目的地");
        $("#searchinput_end").css("color", "#ff7575");
        return;
    }
    //隐藏pickmodal
    //    ISS_MAP.CloseModal();
    $("#pickbar")[0].style.height = "20px";
    $("#map-box")[0].style.height = "calc(100% - 20px)";
    //查询并展示结果
    if (ISS_MAP_SEARCHTYPE == 1) {
        //驾车
        ISS_Map_driving.SearchDrivingTab(0);
    } else if (ISS_MAP_SEARCHTYPE == 2) {
        //步行
        ISS_Map_walk.SearchWalkTab();
    } else if (ISS_MAP_SEARCHTYPE == 3) {
        //公交
        ISS_Map_bus.SearchBusTab(0);
    }
});

//拖动清单事件
//ISS_MAP_Resize($("#dragbar")[0], $("#pickbar")[0]);
var pickheight = true;
$("#dragbar").click(function () {
    if (pickheight) {
        $("#pickbar")[0].style.height = "50px";
        $("#map-box")[0].style.height = "calc(100% - 50px)";
        pickheight = false;
    } else {
        $("#pickbar")[0].style.height = "43%";
        $("#map-box")[0].style.height = "57%";
        pickheight = true;
    }
});
//语音按钮事件
///* 初始化语音config */
//var gn = ['startRecord', 'stopRecord', 'translateVoice', 'openLocation', 'getLocation'];
//wx_config(gn, function () {
//    //callback 所有JSSDK事件
//}, function () {
//    //errorcallback  config注册失败事件
//});
//var voice = {
//    localId: '',
//    serverId: ''
//};
/* 开始录音 */
$(".searchimg").click(function () {
    //标记当前按钮是起点还是终点
    if ($(this).attr("class").indexOf("startvoice") >= 0) {
        ISS_MAP.VoiceBtn = ISS_VOICE_TYPE_ENUM.start;
    } else {
        ISS_MAP.VoiceBtn = ISS_VOICE_TYPE_ENUM.end;
    }
    //弹出层
    myApp.popup('.popup-about');
    parent.ReturnStartVoice();
});
/* 结束录音 */
$$('.close-popup').on('click', function () {
    myApp.closeModal('.popup-about');
    parent.ReturnEndVoid(function (yy) {
        if (ISS_MAP.VoiceBtn == ISS_VOICE_TYPE_ENUM.start) {
            $("#searchinput_start").val(yy);
            ISS_MAP_SEARCH_START.Lng = "";
            ISS_MAP_SEARCH_START.Lat = "";
            ISS_MAP_SEARCH_START.Name = $("#searchinput_start").val();
        } else {
            $("#searchinput_end").val(yy);
            ISS_MAP_SEARCH_END.Lng = "";
            ISS_MAP_SEARCH_END.Lat = "";
            ISS_MAP_SEARCH_END.Name = $("#searchinput_end").val();
        }
    });
});

// 定位对象
function LocalGeocoder(tofrom) {
    var geoc = new BMap.Geocoder();
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var icon = new BMap.Icon("../../assets/images/map/position-icon.png", new BMap.Size(14, 14));
            var mark = new BMap.Marker(r.point, { icon: icon });
            ISS_MAP.nearByMap.addOverlay(mark);
            ISS_MAP.nearByMap.centerAndZoom(r.point, 15);
            //定位成功方法
            ISS_MAP.LocationSuccess(r.point, tofrom);
        } else {
            // 定位失败事件
            ISS_MAP.LocationError(tofrom);
        }
    }, { enableHighAccuracy: true });
}